import type { Settings as LayoutSettings } from '@ant-design/pro-layout';
import { PageLoading } from '@ant-design/pro-layout';
import type { RunTimeLayoutConfig } from 'umi';
import { history, Link, request } from 'umi';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { Login } from '@/pages/Home/service';
// import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import 'antd/dist/antd.css';
const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';

const userinfo = {
  zhName: '古福艳',
  userId: 9346,
  sex: 'F',
  userImage: 'https://shp.qpic.cn/bizmp/v40FKSmkDDLV3Lhbx1vbcDJYr5Jiadu6JpLj4xmicYGQAt76cM0H6Hzw/',
  orgName: '40系统建设部',
  isPunch: 'Y',
  isTs: 'ZB',
  empAttribute: 'P',
  costCenter: 'GZM2120433',
  socialBase: '顺德',
  socialBasePy: 'shunde',
  contractBase: '广州',
  contractBasePy: 'guangzhou',
  telephone: '15112450580',
  baseName: '顺德',
  permissionList: [
    'attendance.afterLeave.saveAfterLeaveConf.create',
    'attendance.afterLeave.findUserCanAfterLeave.search',
  ],
};

/** 获取用户信息比较慢的时候会展示一个 loading */
export const initialStateConfig = {
  loading: <PageLoading />,
};

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState(): Promise<{
  userInfo?: any;
  settings?: Partial<LayoutSettings>;
  currentUser?: API.CurrentUser;
  fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
}> {
  const fetchUserInfo = async () => {
    // try {
      const msg = await Login().then((res) => {
        return res;
      });
      // const msg = {
      //   data: {
      //     name: 'Serati Ma',
      //     avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
      //     userid: '00000001',
      //     email: 'antdesign@alipay.com',
      //     signature: '海纳百川，有容乃大',
      //     title: '交互专家',
      //     group: '蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED',
      //     tags: [
      //       { key: '0', label: '很有想法的' },
      //       { key: '1', label: '专注设计' },
      //       { key: '2', label: '辣~' },
      //       { key: '3', label: '大长腿' },
      //       { key: '4', label: '川妹子' },
      //       { key: '5', label: '海纳百川' },
      //     ],
      //     notifyCount: 12,
      //     unreadCount: 11,
      //     country: 'China',
      //     geographic: {
      //       province: { label: '浙江省', key: '330000' },
      //       city: { label: '杭州市', key: '330100' },
      //     },
      //     address: '西湖区工专路 77 号',
      //     phone: '0752-268888888',
      //   },
      // };
      return msg.data;
    // } catch (error) {
    //   history.push(loginPath);
    // }
    return undefined;
  };
  // // 如果是登录页面，不执行
  // if (history.location.pathname !== loginPath) {
    const currentUser = await fetchUserInfo();
    return {
      userInfo: currentUser,
      fetchUserInfo,
      currentUser,
      settings: {},
    };
  // }
  // return {
  //   fetchUserInfo,
  //   settings: {},
  // };
}

// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout: RunTimeLayoutConfig = ({ initialState }) => {
  return {
    rightContentRender: () => <RightContent />,
    disableContentMargin: false,
    waterMarkProps: {
      content: initialState?.currentUser?.name,
    },
    // footerRender: () => <Footer />,
    onPageChange: () => {
      // const { location } = history;
      // // 如果没有登录，重定向到 login
      // if (!initialState?.currentUser && location.pathname !== loginPath) {
      //   history.push(loginPath);
      // }
    },
    links: isDev
      ? [
          <Link to="/umi/plugin/openapi" target="_blank">
            <LinkOutlined />
            <span>OpenAPI 文档</span>
          </Link>,
          <Link to="/~docs">
            <BookOutlined />
            <span>业务组件文档</span>
          </Link>,
        ]
      : [],
    menuHeaderRender: undefined,
    // 自定义 403 页面
    // unAccessible: <div>unAccessible</div>,
    ...initialState?.settings,
  };
};
